package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.util.Base64Utils;
import com.qf.util.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Base64;

/**
 * 消费者用户管理
 * @author 千锋健哥
 */
@WebServlet("/user")
public class UserController extends BaseController {

    /**
     * 消费者登录
     * @param request
     * @param response
     * @return
     */
    public String login(HttpServletRequest request, HttpServletResponse response) {
        //1. 获取参数
        //用户名
        String username = request.getParameter("username");
        //密码
        String password = request.getParameter("password");
        //消费者页面中输入的验证码
        String code = request.getParameter("code");

        //2. 校验, 用户名, 密码, 验证码不能为空
        if (!StrUtils.empty(username)
                || !StrUtils.empty(password)
                || !StrUtils.empty(code)) {
            return "redirect:/login.jsp";
        }

        //3. 从session中获取验证码
        String sessionCode = (String)request.getSession().getAttribute("code");

        //4. 判断session中的验证码不能为空
        if (!StrUtils.empty(sessionCode) ) {
            return "redirect:/login.jsp";
        }

        //5. 校验消费者输入的验证码和session中的验证码是否一致, 不一致, 不允许登录
        if (!sessionCode.equalsIgnoreCase(code)) {
            return "redirect:/login.jsp";
        }

        //6. 调用service校验用户名, 密码
        UserService userService = new UserServiceImpl();
        User user = userService.login(username, password);

        //7. 判断是否登录成功
        if (user != null) {
            //将用户信息放入session中保存
            request.getSession().setAttribute("loginUser", user);
            return "redirect:/index.jsp";
        } else {
            return "redirect:/login.jsp";
        }
    }

    /**
     * 校验用户名是否存在
     * @param request
     * @param response
     * @return
     */
    public String check(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1. 接收参数, 用户名
        String username = request.getParameter("username");

        //2. 调用service查询这个用户名是否存在
        UserService userService = new UserServiceImpl();
        //用户名存在返回true, 不存在返回false
        boolean flag = userService.checkUserName(username);

        //3. 设置响应头, 返回的数据类型为json
        response.setContentType("application/json;charset=utf-8");

        //4. 将数据返回给页面
        if (flag) {
            //用户名存在
            response.getWriter().write(JSON.toJSONString("1"));
        } else {
            //用户名不存在
            response.getWriter().write(JSON.toJSONString("0"));
        }
        return null;
    }


    /**
     * 消费者注册
     * @param request
     * @param response
     * @return
     */
    public String register(HttpServletRequest request, HttpServletResponse response) {
        //1. 获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String gender = request.getParameter("gender");

        //2. 判断获取的参数是否为空, 如果为空, 不允许注册
        if (!StrUtils.empty(username)) {
            return "redirect:/register.jsp";
        }
        if (!StrUtils.empty(password)) {
            return "redirect:/register.jsp";
        }
        if (!StrUtils.empty(email)) {
            return "redirect:/register.jsp";
        }
        if (!StrUtils.empty(gender)) {
            return "redirect:/register.jsp";
        }

        //3. 调用service, 完成注册
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setGender(gender);
        user.setEmail(email);

        UserService userService = new UserServiceImpl();
        userService.register(user);

        //4. 页面跳转到首页
        return "redirect:/index.jsp";
    }

    /**
     * 激活账户
     * @param request
     * @param response
     * @return
     */
    public String activate(HttpServletRequest request, HttpServletResponse response) {
        //1. 获取参数
        //获取用户邮箱地址
        String e = request.getParameter("e");
        //获取用户激活码
        String c = request.getParameter("c");

        //使用base64对接收到的参数进行解码
        String email = Base64Utils.decode(e);
        String code = Base64Utils.decode(c);

        //2. 调用service进行激活
        UserService userService = new UserServiceImpl();
        userService.active(email, code);

        //3. 跳转到首页
        return "redirect:/index.jsp";

    }
}
